home *** CD-ROM | disk | FTP | other *** search
- Path: noc.netcom.net!news
- From: Tarang Deshpande <tarang@willows.com>
- Newsgroups: comp.lang.c
- Subject: Re: Q: Newbie using text files
- Date: Fri, 19 Apr 1996 12:49:45 -0700
- Organization: NETCOM Network Operations
- Message-ID: <3177EE59.773D@willows.com>
- References: <4l6aqu$rfr@chile.it.earthlink.net>
- NNTP-Posting-Host: daffy.willows.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0GoldB2 (Win95; I)
-
- Peter Bruno wrote:
- >
- > I'm new to C programming so bear with me for a bit...
- >
- > I do a lot of small programming projects at work, mostly to make my
- > life easier, and I've been using QBasic. I would like, however, to
- > use a betting langauage if I could. But, trying to input and
- > processes text files in C seems a lot harder than in C, what with
- > pointers and all.
- >
- > My point, and I do have one, is it worth my time to learn C to process
- > ASCII text files with fixed lenth fields, for sorting etc...
- >
- > and could you suggest some routines for doing this... in basic I just
- > do the following:
- >
- > open "foo.bar" for input as #1
- > open "foo.out" for output as #2
- > while not eof(1)
- > line input #1, row
- > FName = mid$(row, 1, 25) 'characters 1-25 are first name
- > LName = mid$(row, 26, 50)
- > test = mid$(LName, 26, 1)
- > if test < "M" then print #2, row 'if person's last name begins with
- > character less then M print to output file, else we don't want them!
- > wend
- > close #1, #2
- >
- > can someone (kind-hearted and forgiving of stupid questions) please
- > offer suggestions on how to do this in C? PLEASE!
- >
- > thanks,
- >
- > Peter Bruno
- > brunop@earthlink.net
- >
-
- Why are you doing these things in a program anyways? Most O/S's that I've ever
- worked with will allow you to such things using a combination of commands and/or
- standard utilities, not to mention scripting. However if you still want to use
- C then:
-
- /* Note that this program requires the file names of the input and output files
- ** to be on the command line. It also assumes that there is a fixed line length
- ** of 50 characters followed by new line.
- */
-
-
- #include <stdio.h>
-
- int main ( int argc, char **argv )
- {
-
- FILE *Input = NULL;
- FILE *Output = NULL;
- char Name [ 51 ];
-
- if ( ! ( Input = fopen ( argv [ 1 ], "r" ) ) )
- fprintf ( stderr, "Could not open %s\n", argv [ 1 ] );
- else
- if ( ! ( Output = fopen ( argv [ 2 ], "w" ) ) )
- fprintf ( stderr, "Could not open %s\n", argv [ 2 ] );
- else
- while ( ! ( feof ( Input ) ) )
- {
- fread ( Name, sizeof ( Name ), sizeof ( Name ), Input );
- if ( Name [ 25 ] < 'M' )
- fprintf ( "%s", Name );
- }
-
- if ( Input )
- fclose ( Input );
- if ( Output )
- fclose ( Output );
-
- return ( 0 );
-
- }
-
-
-
-
-
- By the way what's a "betting" language and how can processing "text files in
- C seems a lot harder than in C"? :)
-
- Tarang
-